#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    int cherryPickup(vector<vector<int>> &grid)
    {
        int n = grid.size(), m = grid[0].size();
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(m, 0)));
        int ans = (dp[0][0][m - 1] = grid[0][0] + grid[0][m - 1]);
        for (int t = 1; t < n; ++t)
        {
            for (int i = 0; i <= min(t, m - 1); ++i)
            {
                for (int j = m - 1; j >= max(0, m - 1 - t); --j)
                {
                    for (int ni = i - 1; ni <= i + 1; ++ni)
                    {
                        if (ni < 0 || ni >= m)
                            continue;
                        for (int nj = j - 1; nj <= j + 1; ++nj)
                        {
                            if (nj < 0 || nj >= m)
                                continue;
                            dp[t][i][j] = max(dp[t][i][j], dp[t - 1][ni][nj]);
                        }
                    }
                    dp[t][i][j] += grid[t][i] + (i == j ? 0 : grid[t][j]);
                    ans = max(ans, dp[t][i][j]);
                }
            }
        }
        return ans;
    }
};
int main()
{

    // system("pause");
    return 0;
}
